package org.imlinker.login;

import java.io.File;
import java.util.ArrayList;

import org.imlinker.setting.SettingFile;
import org.imlinker.util.PasswordMask;

/**
 * 管理本地帐号。包括利用org.imlinker.setting.SettingFile类从文件中读出信息.
 * 保存到org.imlinker.account.Account类中。同时把一列表的Account类信息保存到指定的本地文件中。
 * 
 * 指定文件是config/accounts.xml
 * 
 * 并对没有设置host,domain,port的帐号填充上默认数据,（现在暂时只有gmail.com等少量的默认数据）。
 */
public class Accounts {

	public Accounts(final String path) {
		String accountFilePath = path + File.separator + "config"
				+ File.separator + "accounts.xml";
		file = new SettingFile("Accounts", accountFilePath);

		// 读取accounts
		list = new ArrayList<Account>();

		int num = file.getElementSize("accounts");
		for (int i = 0; i < num; i++) {
			String accountPath = "accounts.account" + i;

			Account account = new Account();
			account.setUser(file.getValue(accountPath + ".user"));
			account.setPassword(PasswordMask.getPassword(file
					.getValue(accountPath + ".password")));
			account.setDomain(file.getValue(accountPath + ".domain"));
			account.setHost(file.getValue(accountPath + ".host"));
			account.setPort(file.getValue(accountPath + ".port"));

			if (account.getPassword() == null) {
				account.setPassword("");
			}

			list.add(account);
		}

	}

	public Account fillAccount(final Account account) {
		String user = account.getUser();

		String domain = account.getDomain().trim();
		if (domain.length() == 0) {
			account.setDomain(getDomainByUser(user));
		}

		String host = account.getHost().trim();
		if (host.length() == 0) {
			account.setHost(getHostByUser(user));
		}

		String port = account.getPort().trim();
		if (port.length() == 0) {
			account.setPort("5222");
		}

		return account;
	}

	public ArrayList<Account> getAccountList() {
		return list;
	}

	public void saveAccount(final Account account, final boolean savePassword) {
		if (!savePassword) {
			account.setPassword("");
		}

		Account toDelete = null;
		for (Account i : list) {
			if (i.getUser().equals(account.getUser())) {
				toDelete = i;
				break;
			}
		}
		if (toDelete != null) {
			list.remove(toDelete);
		}

		list.add(account);
		saveAccountList();
	}

	private String getDomainByUser(final String user) {
		return user.substring(user.indexOf("@") + 1);
	}

	private String getHostByUser(final String user) {
		String host;

		if (user.endsWith("gmail.com")) {
			host = "talk.google.com";
		} else if (user.endsWith("xiaonei.com")) {
			host = "talk.xiaonei.com";
		} else {
			host = "talk.jabber.org";
		}
		return host;
	}

	private void saveAccountList() {
		file.deleteValue("accounts");

		int size = list.size();
		for (int i = 0; i < size; i++) {
			String path = "accounts.account" + i;
			Account account = list.get(i);

			file.setValue(path + ".user", account.getUser());
			file.setValue(path + ".password", PasswordMask.setPassword(account
					.getPassword()));
			if (account.getHost().trim().length() > 0) {
				file.setValue(path + ".host", account.getHost());
			}
			if (account.getDomain().trim().length() > 0) {
				file.setValue(path + ".domain", account.getDomain());
			}
			if (account.getPort().trim().length() > 0) {
				file.setValue(path + ".port", account.getPort());
			}
		}

		file.save();
	}

	private SettingFile file;

	private ArrayList<Account> list;

}
